using DeviceCommons.DeviceMessages.Enums;

namespace DeviceDataGenerator
{
    /// <summary>
    /// 性能测试配置
    /// </summary>
    public class PerformanceTestSettings
    {
        /// <summary>
        /// 测试配置
        /// </summary>
        public TestSettings Test { get; set; } = new();
        
        /// <summary>
        /// 显示配置
        /// </summary>
        public DisplaySettings Display { get; set; } = new();
        
        /// <summary>
        /// 数据生成配置
        /// </summary>
        public DataGenerationSettings DataGeneration { get; set; } = new();
    }

    /// <summary>
    /// 测试设置
    /// </summary>
    public class TestSettings
    {
        /// <summary>
        /// 测试间隔（毫秒）
        /// </summary>
        public int IntervalMs { get; set; } = 1;
        
        /// <summary>
        /// 最大结果保留数量
        /// </summary>
        public int MaxResultsToKeep { get; set; } = 10000;
        
        /// <summary>
        /// 批量清理数量
        /// </summary>
        public int BatchCleanupCount { get; set; } = 1000;
        
        /// <summary>
        /// 加密概率（0.0-1.0）
        /// </summary>
        public double EncryptionProbability { get; set; } = 0.5;
        
        /// <summary>
        /// 压缩概率（0.0-1.0）
        /// </summary>
        public double CompressionProbability { get; set; } = 0.5;
        
        /// <summary>
        /// 子设备概率（0.0-1.0）
        /// </summary>
        public double ChildDeviceProbability { get; set; } = 0.3;
    }

    /// <summary>
    /// 显示设置
    /// </summary>
    public class DisplaySettings
    {
        /// <summary>
        /// 统计更新间隔（毫秒）
        /// </summary>
        public int UpdateIntervalMs { get; set; } = 2000;
        
        /// <summary>
        /// 是否显示详细信息
        /// </summary>
        public bool ShowDetailedStats { get; set; } = true;
        
        /// <summary>
        /// 是否显示实时图表
        /// </summary>
        public bool ShowRealtimeChart { get; set; } = false;
        
        /// <summary>
        /// 数值显示精度
        /// </summary>
        public int DecimalPlaces { get; set; } = 3;
    }

    /// <summary>
    /// 数据生成设置
    /// </summary>
    public class DataGenerationSettings
    {
        /// <summary>
        /// 设备名前缀列表
        /// </summary>
        public string[] DeviceNamePrefixes { get; set; } = 
        {
            "Sensor", "Device", "Gateway", "Monitor", "Controller", 
            "Node", "Hub", "Station", "Unit", "Probe"
        };
        
        /// <summary>
        /// 读数数量范围
        /// </summary>
        public Range ReadingCountRange { get; set; } = new(1, 21);
        
        /// <summary>
        /// 状态数量范围
        /// </summary>
        public Range StateCountRange { get; set; } = new(1, 11);
        
        /// <summary>
        /// 子设备数量范围
        /// </summary>
        public Range ChildDeviceCountRange { get; set; } = new(1, 4);
        
        /// <summary>
        /// 子设备读数数量范围
        /// </summary>
        public Range ChildReadingCountRange { get; set; } = new(1, 6);
        
        /// <summary>
        /// 子设备状态数量范围
        /// </summary>
        public Range ChildStateCountRange { get; set; } = new(1, 6);
        
        /// <summary>
        /// 字符串长度范围
        /// </summary>
        public Range StringLengthRange { get; set; } = new(1, 51);
        
        /// <summary>
        /// 二进制数据长度范围
        /// </summary>
        public Range BinaryLengthRange { get; set; } = new(1, 101);
        
        /// <summary>
        /// 密码长度范围
        /// </summary>
        public Range PasswordLengthRange { get; set; } = new(8, 17);
        
        /// <summary>
        /// 时间偏移范围
        /// </summary>
        public Range TimeOffsetRange { get; set; } = new(-1000, 1001);
        
        /// <summary>
        /// 状态值类型权重
        /// </summary>
        public StateTypeWeights StateTypeWeights { get; set; } = new();
    }

    /// <summary>
    /// 范围定义
    /// </summary>
    public class Range
    {
        public int Min { get; set; }
        public int Max { get; set; }
        
        public Range() { }
        
        public Range(int min, int max)
        {
            Min = min;
            Max = max;
        }
        
        public int GetRandomValue(Random random)
        {
            return random.Next(Min, Max);
        }
    }

    /// <summary>
    /// 状态值类型权重
    /// </summary>
    public class StateTypeWeights
    {
        public int String { get; set; } = 25;
        public int Binary { get; set; } = 15;
        public int Int32 { get; set; } = 20;
        public int Int16 { get; set; } = 10;
        public int UInt16 { get; set; } = 5;
        public int Float32 { get; set; } = 15;
        public int Double { get; set; } = 5;
        public int Bool { get; set; } = 5;
        
        public int TotalWeight => String + Binary + Int32 + Int16 + UInt16 + Float32 + Double + Bool;
        
        public StateValueTypeEnum GetRandomType(Random random)
        {
            var value = random.Next(TotalWeight);
            var cumulative = 0;
            
            if ((cumulative += String) > value) return StateValueTypeEnum.String;
            if ((cumulative += Binary) > value) return StateValueTypeEnum.Binary;
            if ((cumulative += Int32) > value) return StateValueTypeEnum.Int32;
            if ((cumulative += Int16) > value) return StateValueTypeEnum.Int16;
            if ((cumulative += UInt16) > value) return StateValueTypeEnum.UInt16;
            if ((cumulative += Float32) > value) return StateValueTypeEnum.Float32;
            if ((cumulative += Double) > value) return StateValueTypeEnum.Double;
            
            return StateValueTypeEnum.Bool;
        }
    }

    /// <summary>
    /// 默认配置提供者
    /// </summary>
    public static class DefaultConfig
    {
        public static PerformanceTestSettings GetDefault()
        {
            return new PerformanceTestSettings();
        }
        
        /// <summary>
        /// 高强度测试配置
        /// </summary>
        public static PerformanceTestSettings GetHighIntensity()
        {
            return new PerformanceTestSettings
            {
                Test = new TestSettings
                {
                    IntervalMs = 0,
                    EncryptionProbability = 0.7,
                    CompressionProbability = 0.7,
                    ChildDeviceProbability = 0.5
                },
                DataGeneration = new DataGenerationSettings
                {
                    ReadingCountRange = new(10, 51),
                    StateCountRange = new(5, 21),
                    StringLengthRange = new(20, 101),
                    BinaryLengthRange = new(50, 201)
                }
            };
        }
        
        /// <summary>
        /// 低强度测试配置
        /// </summary>
        public static PerformanceTestSettings GetLowIntensity()
        {
            return new PerformanceTestSettings
            {
                Test = new TestSettings
                {
                    IntervalMs = 10,
                    EncryptionProbability = 0.2,
                    CompressionProbability = 0.2,
                    ChildDeviceProbability = 0.1
                },
                DataGeneration = new DataGenerationSettings
                {
                    ReadingCountRange = new(1, 6),
                    StateCountRange = new(1, 4),
                    StringLengthRange = new(5, 21),
                    BinaryLengthRange = new(5, 51)
                }
            };
        }
        
        /// <summary>
        /// 压力测试配置
        /// </summary>
        public static PerformanceTestSettings GetStressTest()
        {
            return new PerformanceTestSettings
            {
                Test = new TestSettings
                {
                    IntervalMs = 0,
                    EncryptionProbability = 1.0,
                    CompressionProbability = 1.0,
                    ChildDeviceProbability = 0.8,
                    MaxResultsToKeep = 50000
                },
                DataGeneration = new DataGenerationSettings
                {
                    ReadingCountRange = new(20, 101),
                    StateCountRange = new(10, 51),
                    ChildDeviceCountRange = new(3, 11),
                    StringLengthRange = new(50, 251),
                    BinaryLengthRange = new(100, 256)
                }
            };
        }
    }
}